package com.magicsoft.share.ui.main;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.main.User;
import com.magicsoft.share.constant.AppDavikActivityMgr;
import com.magicsoft.share.constant.Constant;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.customview.alertview.AlertView;
import com.magicsoft.share.customview.alertview.OnItemClickListener;
import com.magicsoft.share.presenter.main.InputPasswordPresenter;
import com.magicsoft.share.ui.my.SettingActivity;
import com.magicsoft.share.utils.AlerterUtils;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.utils.Validator;
import com.magicsoft.share.view.main.IInputPasswordView;

import butterknife.BindView;
import butterknife.OnClick;

public class InputPasswordActivity extends BaseActivity<IInputPasswordView, InputPasswordPresenter> implements IInputPasswordView {

    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.edt_password)
    EditText edtPassword;
    @BindView(R.id.edt_confirm_password)
    EditText edtConfirmPassword;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    private String passwordStr;
    private String confirmPasswordStr;
    private AlertView mAlertViewExt;
    private int inputPasswordType;
    private String mobileStr;
    private String codeStr;
    private String oldPassword;


    @Override
    public InputPasswordPresenter initPresenter() {
        return new InputPasswordPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_input_password;
    }

    @Override
    public void initData() {
        inputPasswordType = getIntent().getIntExtra("inputPasswordType", -1);
        mobileStr = getIntent().getStringExtra("mobileStr");
        codeStr = getIntent().getStringExtra("codeStr");
        oldPassword = getIntent().getStringExtra("oldPassword");
    }

    @Override
    public void initView() {
        if (inputPasswordType == Constant.INPUT_PASSWORD_MODIFY) {
            tvTitle.setText("设置新密码");
            btnConfirm.setText("确认修改");
        } else {
            tvTitle.setText("输入密码");
            btnConfirm.setText("注册");
        }


    }


    @Override
    public void requestData() {

    }

    @OnClick({R.id.ibtn_left, R.id.btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            case R.id.btn_confirm:
                register();
                break;
        }
    }

    private void register() {
        passwordStr = edtPassword.getText().toString();
        confirmPasswordStr = edtConfirmPassword.getText().toString();
        if (TextUtils.isEmpty(passwordStr)) {
            AlerterUtils.warning(this, "请输入密码");
        } else if (TextUtils.isEmpty(confirmPasswordStr)) {
            AlerterUtils.warning(this, "请再次输入密码");
        } else if (!passwordStr.equals(confirmPasswordStr)) {
            AlerterUtils.warning(this, "密码不一致，请重新输入");
        } else if (!Validator.isPassword(passwordStr) || !Validator.isPassword(passwordStr)) {
            AlerterUtils.warning(this, "密码格式错误，请重新输入");
        } else {
            if (inputPasswordType == Constant.INPUT_PASSWORD_MODIFY) {

                mPresenter.editUserDetail(oldPassword, passwordStr);

            } else {
                mPresenter.register(mobileStr, passwordStr, codeStr);
            }

        }

    }

    private void modifySuccess() {
        mAlertViewExt = new AlertView("修改成功", "是否直接登录", "否", null, new String[]{"是"}, this, AlertView.Style.Alert, new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {

                if (position == -1) {

                    AppDavikActivityMgr.getScreenManager().finishActivity(MainActivity.class);
                    AppDavikActivityMgr.getScreenManager().finishActivity(SettingActivity.class);
                    SPUtils.getInstance().clear();
                    startActivity(new Intent(InputPasswordActivity.this, LoginActivity.class));
                    finish();

                } else {
                    mPresenter.login(SPUtils.getInstance().getString(SPKey.MOBILE), passwordStr);
                }
            }
        });
        mAlertViewExt.show();

    }

    @Override
    public void registerSuccess() {
        mAlertViewExt = new AlertView("注册成功", "是否直接登录", "否", null, new String[]{"是"}, this, AlertView.Style.Alert, new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {

                if (position == -1) {
                    finish();

                } else {
                    mPresenter.login(mobileStr, passwordStr);
                }
            }
        });
        mAlertViewExt.show();
    }

    @Override
    public void loginSuccess(User user) {
        saveUserIfo(user);
        AppDavikActivityMgr.getScreenManager().finishActivity(LoginActivity.class);
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void editUserDetailSuccess() {
        modifySuccess();
    }

    private void saveUserIfo(User user) {
        SPUtils.getInstance().put(SPKey.ID, user.getId());
        SPUtils.getInstance().put(SPKey.TOKEN, user.getToken());
        SPUtils.getInstance().put(SPKey.TOKEN, user.getPhone());
    }
}
